package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;

public class OtherTeacherDao implements BaseTeacherDao {
    private static ArrayList<Teacher> teac = new ArrayList<>();

    static {
        System.out.println("----------------Loding----------------");

    }

    public boolean addTeacher(Teacher tea) {
        teac.add(tea);
        reSave01();
        return true;
    }

    public Teacher[] findAllTeacher() {
        return teac.toArray(new Teacher[teac.size()]);
    }

    public void deleteTeacherById(String delId) {
        int index = getIndex(delId);
        teac.remove(index);
        reSave01();
    }

    public int getIndex(String delId) {
        int index = -1;
        for (int i = 0; i < teac.size(); i++) {
            Teacher t = teac.get(i);
            if (t != null && t.getId().equals(delId)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(String updateId, Teacher newtea) {
        int index = getIndex(updateId);
        teac.set(index, newtea);
        reSave01();
    }

    public void reSave01() {
        //创建字节输出流对象
        try {
            FileOutputStream fos = new FileOutputStream("itheima-edu-info-manager\\teacher.txt");
            for (int i = 0; i < teac.size(); i++) {
                Teacher tea = teac.get(i);
                fos.write(tea.toString().getBytes());
                fos.write(System.lineSeparator().getBytes());
            }
            fos.close();
        } catch (Exception e) {
        }
    }

    public static void load01() {
        try {
            FileInputStream fis = new FileInputStream("itheima-edu-info-manager\\stu.txt");
            byte[] bys = new byte[1024*1024];
            int len = fis.read(bys);

            if(len != -1) {
                String s = new String(bys, 0, len);

                String[] tea = s.split(System.lineSeparator());
                for (String allTea : tea) {
                    String[] split = allTea.split(",");
                    Teacher d = new Teacher(split[0], split[1], split[2], split[3]);
                    teac.add(d);
                }
            }
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void load02(){
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("itheima-edu-info-manager\\stu.txt"));
            byte[] bys = new byte[1024*1024];
            int len;
            while((len = bis.read(bys))!=-1){
                String s = new String(bys,0,len);
                String[] split = s.split(System.lineSeparator());
                for (String tea : split) {
                    String[] allTea = tea.split(",");
                    Teacher d = new Teacher(allTea[0],allTea[1],allTea[2],allTea[3]);
                    teac.add(d);
                }
            }
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
